package org.jxkj.app

import java.sql.{DriverManager, ResultSet}
import java.util.Properties


object MysqlDml {
  //edos
  var url      = ""
  val driver   = "com.mysql.cj.jdbc.Driver"
  var user     = ""
  var password = ""
  //hmsdb
  var url2      = ""
  val driver2   = "com.mysql.cj.jdbc.Driver"
  var user2     = ""
  var password2= ""
  //
  var fileprefix=""

  def delete(args: String): Unit = {
    val prop = new Properties
    prop.setProperty("user",user)
    prop.setProperty("password",password)
    prop.setProperty("driver",driver)
    prop.setProperty("charset","UTF-8")
    try {
    Class.forName(driver)
    val conn = DriverManager.getConnection(url,prop)
    //val sm = conn.prepareCall("truncate table "+tableName)
    //sm.execute()
    //sm.close()
    val statement = conn.createStatement
    //    执行删除操作
    //"delete from persons WHERE `name`=\"徐志摩\""
    println(args)
    val rs = statement.executeUpdate(args)

    println("删除数据完成！"+rs)
    conn.close()
  } catch {
    case e: Exception => e.printStackTrace
  }
  }

  def delete2(args: String): Unit = {
    val prop = new Properties
    prop.setProperty("user",user2)
    prop.setProperty("password",password2)
    prop.setProperty("driver",driver)
    prop.setProperty("charset","UTF-8")
    try {
      Class.forName(driver)
      val conn = DriverManager.getConnection(url2,prop)
      //val sm = conn.prepareCall("truncate table "+tableName)
      //sm.execute()
      //sm.close()
      val statement = conn.createStatement
      //    执行删除操作

      println(args)
      val rs = statement.executeUpdate(args)

      println("删除数据完成！"+rs)
      conn.close()
    } catch {
      case e: Exception => e.printStackTrace
    }
  }

  def insert(rpt_desc:String,opt_start_time:String,opt_end_time:String,run_status:String,start_date:String,end_date:String): Unit = {
    val prop = new Properties
    prop.setProperty("user",user)
    prop.setProperty("password",password)
    prop.setProperty("driver",driver)
    prop.setProperty("charset","UTF-8")
    try {
      Class.forName(driver)
      val conn = DriverManager.getConnection(url,prop)
      //val sm = conn.prepareCall("truncate table "+tableName)
      //sm.execute()
      //sm.close()
      val sqlstring= s"insert into t_scala_log values('$rpt_desc','$opt_start_time','$opt_end_time','$run_status','$start_date','$end_date',timestampdiff(MINUTE,'$opt_start_time','$opt_end_time'))"
      println(sqlstring)
      val statement = conn.createStatement
      val rs = statement.executeUpdate(sqlstring)

      println("insert 完成！"+rs)
      conn.close()
    } catch {
      case e: Exception => e.printStackTrace
    }
  }

  def insert2(task_name:String,task_no:String,scene_id:String,scene_name:String,status:String,start_time:String,end_time:String,total:Int,message:String,begin_time:String,finish_time:String): Unit = {
    val prop = new Properties
    prop.setProperty("user",user)
    prop.setProperty("password",password)
    prop.setProperty("driver",driver)
    prop.setProperty("charset","UTF-8")
    try {
      Class.forName(driver)
      val conn = DriverManager.getConnection(url2,prop)

      val sqlstring= s"insert into hms_io_analyse_log(task_name,task_no,scene_id,scene_name,status,start_time,end_time,total,message,begin_time,finish_time,time_cost)" +
        s" values('$task_name','$task_no','$scene_id','$scene_name','$status','$start_time','$end_time','$total','$message','$begin_time','$finish_time',timestampdiff(SECOND,'$begin_time','$finish_time'))"
      println(sqlstring)
      val statement = conn.createStatement
      val rs = statement.executeUpdate(sqlstring)

      println("insert 完成！"+rs)
      conn.close()
    } catch {
      case e: Exception => e.printStackTrace
    }
  }

}
